<?php
class CouponorderAction extends CommonAction {
    //过滤查询字段
    function _filter(&$map){
        if(I('s_status')){
            $map['status'] = array('eq',(I('s_status')-1));
            $this->assign('s_status',I('s_status'));
        }
        if(I('s_order_id')){
        	$map['order_id'] = array('like',"%".I('s_order_id')."%");
        	$this->assign('s_order_id',I('s_order_id')); 
        }

        if(I('s_start_time')){
        	$map['create_time'] = array('egt',strtotime(I('s_start_time')));
        	$this->assign('s_start_time',strtotime(I('s_start_time')));
        }
         
        if(I('s_end_time')){
        	$map['create_time'] = array('elt',strtotime(I('s_end_time')));
        	$this->assign('s_end_time',strtotime(I('s_end_time')));
        }
         
        if(I('s_start_time') && I('s_end_time')){
        	$map['create_time'] = array(array('egt',strtotime(I('s_start_time'))),array('elt',strtotime(I('s_end_time'))),'and');
        }
        
        if(I('s_type_id')){
        	$map['type_id'] = array('eq',I('s_type_id'));
        	$this->assign('s_type_id',I('s_type_id'));
        }

        if(I('province')){
        	$map['province'] = array('eq',I('province'));
        	$this->assign('province',I('province'));
        }else{
        	unset($map['province']);
        }
        
        if(I('city')){
        	$map['city'] = array('eq',I('city'));
        	$this->assign('city',I('city'));
        }else{
        	unset($map['city']);
        }
        
        if(I('town')){
        	$map['town'] = array('eq',I('town'));
        	$this->assign('town',I('town'));
        }else{
        	unset($map['town']);
        }
        
        if(I('s_mobile')){
        	$map['mobile'] = array('like',"%".I('s_mobile')."%");
        	$this->assign('s_mobile',I('s_mobile'));
        }
        
        if(I('s_number')){
        	$coupon_id = M('Coupon')->where('number like "%'.I('s_number').'%"')->getField('id');
        	$map['coupon_id'] = array('eq',$coupon_id);
        	$this->assign('s_number',I('s_number'));
        }
		
		if(I('s_price')){
        	$coupon_id = M('Coupon')->where('price = '.I('s_number'))->getField('id');
        	$map['coupon_id'] = array('eq',$coupon_id);
        	$this->assign('s_number',I('s_number'));
        }
        
        if(I('s_type_id')){
        	$map['type_id'] = array('eq',I('s_type_id'));
        	$this->assign('s_type_id',I('s_type_id'));
        }
		
		if(I('s_merchant_id')){
			//查询IDS
			$coupon_id_arr = M('Coupon')->where('merchant_id = '.I('s_merchant_id').' and status=1')->getField('id',true);
			$coupon_ids = implode(',',$coupon_id_arr);
        	$map['coupon_id'] = array('in',$coupon_ids);
        	$this->assign('s_merchant_id',I('s_merchant_id'));
        }
    }
    
    function index(){
    	$type_list = coupon_type();
    	$this->assign('type_list',$type_list);
		
		$price_list = coupon_price();
    	$this->assign('price_list',$price_list);
    	
		$merchant_list = getMerchant();
		$this->assign('merchant_list',$merchant_list);
		
    	$province_list = M('Region')->where ( array('pid'=>1) )->select();
    	$this->assign('province_list',$province_list);
    	
    	//列表过滤器，生成查询Map对象
    	$map = $this->_search();
    	if (method_exists($this, '_filter')) {
    		$this->_filter($map);
    	}

    	$name = $this->getActionName();
    	
    	$model = D($name);
    	if (!empty($model)) {
    		 
    		//排序字段 默认为主键名
    		if (isset($_REQUEST ['_order'])) {
    			$order = $_REQUEST ['_order'];
    		} else {
    			$order = !empty($sortBy) ? $sortBy : $model->getPk();
    		}
    		
    		//排序方式默认按照倒序排列
    		//接受 sost参数 0 表示倒序 非0都 表示正序
    		if (isset($_REQUEST ['_sort'])) {
    			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
    		} else {
    			$sort = $asc ? 'asc' : 'desc';
    		}
    		//取得满足条件的记录数
    		$count = $model->where($map)->count('id');

    		if ($count > 0) {
    			import("@.ORG.Util.Page");
    			//创建分页对象
    			if (!empty($_REQUEST ['listRows'])) {
    				$listRows = $_REQUEST ['listRows'];
    			} else {
    				$listRows = '';
    			}
    			$p = new Page($count, $listRows);
    		
    			//分页查询数据
    			if(I('export')){
    				//到处不分页
    				$voList = $model->where($map)->order("`" . $order . "` " . $sort)->select();
    			}else{
    				$voList = $model->where($map)->order("`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select();
    			}

    			//分页跳转的时候保证查询条件
    			foreach ($map as $key => $val) {
	    			if (!is_array($val)) {
	                    $p->parameter .= "$key=" . urlencode($val) . "&";
	                }else{
	                	$p->parameter .= "&$key=" . urlencode($val[1]);
	                }
    			}

    			//分页显示
    			$page = $p->show();
    			//列表排序显示
    			$sortImg = $sort; //排序图标
    			$sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
    			$sort = $sort == 'desc' ? 1 : 0; //排序方式
    			//模板赋值显示
    			$this->assign('list', $voList);
    			$this->assign('sort', $sort);
    			$this->assign('order', $order);
    			$this->assign('sortImg', $sortImg);
    			$this->assign('sortType', $sortAlt);
    			$this->assign("page", $page);
    		}
    		Cookie::set('_currentUrl_', __SELF__);
    	}
    	
    	if(I('export')){
    		
    		$content = iconv("utf-8","gbk","订单编号,货券类型,货券价格,货券编号,销售商,订购时间,订购姓名,订购手机,订购省份,订购城市,订购地区,订购地址,订购状态,激活状态,物流单号,用户备注,商家备注,来自平台");
    		$content = $content . "\n";
    		
    		$order_value=array();
    		foreach($voList as $vo){

    			$order_value['order_id'] = '"' . iconv('utf-8','gbk',$vo['order_id']) . '"';

                //获取订单信息
                $coupon_info = M('Coupon')->where('id='.$vo['coupon_id'])->field('number,type_id,merchant_id')->find();
                $coupon_type = coupon_type($coupon_info['type_id']);
				$coupon_price = coupon_price($coupon_info['type_id']);
                $coupon_number = $coupon_info['number'];
                $coupon_merchant = getMerchantName($coupon_info['merchant_id']);

    			$order_value['coupon_type'] = '"' . iconv('utf-8','gbk',$coupon_type) . '"';
				$order_value['coupon_price'] = '"' . iconv('utf-8','gbk',$coupon_price) . '"';
    			$order_value['coupon_number'] = '"' . iconv('utf-8','gbk',$coupon_number) . '"';
                $order_value['coupon_merchant'] = '"' . iconv('utf-8','gbk',$coupon_merchant) . '"';
    			$order_value['create_time'] = '"' . iconv('utf-8','gbk',toDate($vo['create_time'],'y-m-d H:i')) . '"';

    			$order_value['name'] = '"' . iconv('utf-8','gbk',$vo['name']) . '"';
    			$order_value['mobile'] = '"' . iconv('utf-8','gbk',$vo['mobile']) . '"';
    			$order_value['province'] = '"' . iconv('utf-8','gbk',getRegion($vo['province'])) . '"';
    			$order_value['city'] = '"' . iconv('utf-8','gbk',getRegion($vo['city'])) . '"';
    			$order_value['town'] = '"' . iconv('utf-8','gbk',getRegion($vo['town'])) . '"';
    			$order_value['address'] = '"' . iconv('utf-8','gbk',$vo['address']) . '"';
    			$order_value['status'] = '"' . iconv('utf-8','gbk',getCouponorderStatusS($vo['status'])) . '"';
    			$order_value['coupon_status'] = '"' . iconv('utf-8','gbk',getOrderCouponStatusS($vo['coupon_id'])) . '"';
    			$order_value['deliveryno'] = '"' . iconv('utf-8','gbk',$vo['deliveryno']) . '"';
    			$order_value['remark'] = '"' . iconv('utf-8','gbk',$vo['remark']) . '"';
    			$order_value['userremark'] = '"' . iconv('utf-8','gbk',$vo['userremark']) . '"';
    			$order_value['is_wap'] = '"' . iconv('utf-8','gbk',getWapStatus($vo['is_wap'])) . '"';
    			
    			$content .= implode(",", $order_value) . "\n";
    		}
    		
    		header("Content-Disposition: attachment; filename=order_list.csv");
    		echo $content;exit;
    	}
    	
    	$this->display();
    }
    
    public function edit(){
    	$name = $this->getActionName();
    	$model = M($name);
    	$id = $_REQUEST [$model->getPk()];
    	
    	if(!$id)
    		$this->error("参数错误！");
    	
    	$vo = $model->getById($id);
    	
    	$coupon_info = M('Coupon')->find($vo['coupon_id']);
    	
    	$delivery_list = getDeliverytype();
    	
    	$this->assign('vo', $vo);
    	$this->assign('coupon_info', $coupon_info);
    	$this->assign('delivery_list', $delivery_list);
    	$this->display();
    }
    
    public function check(){
    	$data['deliverytype'] = $deliverytype = I('deliverytype');
    	$data['deliveryno'] = $deliveryno = I('deliveryno');
    	$data['userremark'] = $userremark = I('userremark');
    	$data['id'] = I('id');
    	$data['send_time'] = time();
    	$data['status'] = 1;
    	
		if(!$deliveryno && !$userremark){
			$this->error('物流单号或发货备注必填！');
		}
		$savereturn = M('Couponorder')->save($data);
		
		if($savereturn !== false){
			$this->error('发货成功！',U('Couponorder/index',array('i'=>rand(10000,99999))));
		}else{
			$this->error('发货失败！');
		}
    }

    public function check_edit(){
    	$deliverytype = I('deliverytype');
        $userremark = I('userremark');
        $deliveryno = I('deliveryno');
        $id = I('id');
		$data = array('userremark'=>$userremark,'deliveryno'=>$deliveryno,'deliverytype'=>$deliverytype);
        $savereturn = M('Couponorder')-> where('id='.$id)->setField($data);

        //v($savereturn);
        if($savereturn !== false){
            $this->error('修改备注成功！',U('Couponorder/index',array('i'=>rand(10000,99999))));
        }else{
            $this->error('修改备注失败！');
        }
    }
    
    public function over(){
    	 
    	$data['id'] = $id = I('id');
    	$data['check_time'] = time();
    	$data['status'] = 3;
    	if(!$id){
    		$this->error('参数错误！');
    	}
    	
    	$coupon_info = M('Coupon')->join('rc_couponorder ON rc_coupon.id=rc_couponorder.coupon_id')->where('rc_couponorder.id='.$id)->getField('rc_coupon.is_actived');
    	
    	if(!$coupon_info) $this->error('请先激活相关提货券！');
    	
    	$savereturn = M('Couponorder')->save($data);
    		
    	if($savereturn){
    		$this->error('审核成功！',U('Couponorder/index',array('i'=>rand(10000,99999))));
    	}else{
    		$this->error('审核失败请联系管理员！');
    	}
    }
    
    public function cancle(){
    
    	$data['id'] = $id = I('id');
    	$data['cancle_time'] = time();
    	$data['status'] = 0;
    	if(!$id){
    		$this->error('参数错误！');
    	}
    	
    	$savereturn = M('Couponorder')->save($data);
    
    	if($savereturn){
    		$this->error('取消成功！',U('Couponorder/index',array('i'=>rand(10000,99999))));
    	}else{
    		$this->error('取消失败请联系管理员！');
    	}
    }
    
    public function nocancle(){
    
    	$data['id'] = $id = I('id');
    	$data['cancle_time'] = '';

    	$data['status'] = 2;	//恢复到未审核状态 重新开始
    	$data['deliverytype'] = 0;
    	$data['deliveryno'] = '';
    	$data['userremark'] = '';
    	$data['check_time'] = '';
    	$data['send_time'] = '';
    	
    	if(!$id){
    		$this->error('参数错误！');
    	}
    	 
    	$savereturn = M('Couponorder')->save($data);
    
    	if($savereturn){
    		$this->error('恢复成功！',U('Couponorder/index',array('i'=>rand(10000,99999))));
    	}else{
    		$this->error('恢复失败请联系管理员！');
    	}
    }
    
    public function address_edit(){
    	$id = I('id');
    	$order_info = M('Couponorder')-> where('id='.$id)->find();
    	
    	$province = M('Region')->where ( array('pid'=>1) )->select();
    	$this->assign('province',$province);
    	
    	//v($order_info);
    	$this->assign('vo', $order_info);
    	$this->display();
    }
    
    public function address_edit_insert(){
    	
    	$data['id'] = $id = I('id');
    	
    	$data['status'] = 2;	//恢复到未审核状态 重新开始
    	$data['deliverytype'] = 0;
    	$data['deliveryno'] = '';
    	$data['userremark'] = '';
    	$data['check_time'] = '';
    	$data['send_time'] = '';
    	
    	$data['name'] = I('name');
    	$data['mobile'] = I('mobile');
    	$data['province'] = I('province');
    	$data['city'] = I('city');
    	$data['town'] = I('town');
    	$data['address'] = I('address');
    	$data['remark'] = I('remark');
     	//v($data);
     	
     	$savereturn = M('Couponorder')->save($data);
    	
    	//v($savereturn);
    	if($savereturn !== false){
    		$this->error('修改配送信息成功！',U('Couponorder/edit',array('id'=>$id,'i'=>rand(10000,99999))));
    	}else{
    		$this->error('修改配送信息失败！');
    	}
    }
}
?>